package com.zrrd.mapper;

import com.zrrd.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {

    String SELECT_ALL = "SELECT U.ID, U.USERNAME, U.PASSWORD, U.REAL_NAME, U.NICK_NAME, U.GENDER, " +
            "U.AGE, U.ID_CARD, U.AVATAR, U.PHONE, U.INFO, U.CREATE_TIME, U.LAST_MODIFY " +
            "FROM  lesson.user U ";

    /**
     * 单增User记录
     *
     * @param user 用户实体
     * @return 影响条目数
     */
    @Insert("INSERT INTO  lesson.user (USERNAME, PASSWORD, REAL_NAME, NICK_NAME, GENDER, " +
            "AGE, ID_CARD, AVATAR, PHONE, INFO, CREATE_TIME, LAST_MODIFY) " +
            "VALUES (#{username}, #{password}, #{realName}, #{nickName}, #{gender}, " +
            "#{age}, #{idCard}, #{avatar}, #{phone}, #{info}, #{createTime,jdbcType=DATE}, #{lastModify,jdbcType=DATE})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 按主键单改User记录：其中ID，USERNAME，REAL_NAME，ID_CARD和CREATE_TIME字段不允许修改
     *
     * @param user 用户实体
     * @return 影响条目数
     */
    @Update("<script>" +
            "UPDATE lesson.user" +
            "<set> " +
            "<if test='password != null'> PASSWORD = #{password}, </if>" +
            "<if test='nickName != null'> NICK_NAME = #{nickName}, </if>" +
            "<if test='gender != null'> GENDER = #{gender}, </if>" +
            "<if test='age != null'> AGE = #{age}, </if>" +
            "<if test='avatar != null'> AVATAR = #{avatar}, </if>" +
            "<if test='phone != null'> PHONE = #{phone}, </if>" +
            "<if test='info != null'> INFO = #{info}, </if>" +
            "LAST_MODIFY = now()" +
            "</set>" +
            "<where>" +
            "<if test='id != null'> ID = #{id} </if> OR 1 = 2" +
            "</where>" +
            "</script>")
    int updateById(User user);

    /**
     * 按主键单删User记录
     *
     * @param id User表主键
     * @return 影响条目数
     */
    @Delete("<script>" +
            "DELETE FROM lesson.user" +
            "<where>" +
            "<if test='_parameter != null'> ID = #{param1} </if> " +
            "OR 1 = 2 " +
            "</where>" +
            "</script>")
    int deleteById(Integer id);

    /**
     * 按主键单查User记录
     *
     * @param id User表主键
     * @return 单条User记录
     */
    @Select("<script>" +
            SELECT_ALL +
            "<where>" +
            "<if test='_parameter != null'> U.ID = #{param1} </if> " +
            "OR 1 = 2" +
            "</where>" +
            "</script>")
    User selectById(Integer id);

    /**
     * 全查User记录
     *
     * @return 全部User记录
     */
    @Select(SELECT_ALL)
    List<User> list();

    /**
     * 按账号密码单查User记录
     *
     * @param username 登录账号
     * @param password 登录密码
     * @return 单条User记录
     */
    @Select("<script>" +
            SELECT_ALL +
            "<where>" +
            "<if test='arg0 != null and arg1 != null'> U.USERNAME = #{param1} AND U.PASSWORD = #{param2} </if> " +
            "OR 1 = 2" +
            "</where>" +
            "</script>")
    User selectByUsernameAndPassword(String username, String password);
    /**
     * 按手机号码单查User记录
     *
     * @param phone 手机号码
     * @return 单条User记录
     */
    @Select("<script>" +
            SELECT_ALL +
            "<where>" +
            "<if test='_parameter != null'> U.PHONE = #{param1} </if> " +
            "OR 1 = 2" +
            "</where>" +
            "</script>")
    User selectByPhone(String phone);
}
